/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.finalresults;

import data.ACStorage;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Djordje Gligorijevic
 */
public class FrmFinalResults extends javax.swing.JDialog {

    /**
     * Creates new form FrmFinalResults
     */
    public FrmFinalResults(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        
        if(ACStorage.getInstance().getGoal() != null){
            PnlFinalResultsTable pfrt = new PnlFinalResultsTable();
            pfrt.getTblFinalResults().setModel(new TblModelFinalResults(ACStorage.getInstance().getGoal()));
            setActivePanel(pfrt);
        }
        else{
            JOptionPane.showMessageDialog(this, "Mile Kitić bi znao da nema podataka sa kojima bi moglo da se radi.","Mile", JOptionPane.ERROR_MESSAGE);
        }
        this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("images/fon-a.png")));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlFinalResults = new javax.swing.JPanel();
        btnCalculateFinalResults = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Final Results");

        pnlFinalResults.setBorder(javax.swing.BorderFactory.createTitledBorder("Final results"));
        pnlFinalResults.setLayout(new java.awt.BorderLayout());
        getContentPane().add(pnlFinalResults, java.awt.BorderLayout.CENTER);

        btnCalculateFinalResults.setText("Calculate final results");
        btnCalculateFinalResults.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCalculateFinalResultsActionPerformed(evt);
            }
        });
        getContentPane().add(btnCalculateFinalResults, java.awt.BorderLayout.PAGE_START);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCalculateFinalResultsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCalculateFinalResultsActionPerformed
        //TODO izracnati konacne vrednosti:
        for (int i = 0; i < ACStorage.getInstance().getGoal().getListAlternative().size(); i++) {
            double mark = 0;
            for (int j = 0; j < ACStorage.getInstance().getGoal().getListCriteria().size(); j++) {
                mark += (ACStorage.getInstance().getGoal().getListCriteria().get(j).getAverageValue().get(i)
                        * ACStorage.getInstance().getGoal().getCriteriaPonders().get(j));
            }
            ACStorage.getInstance().getGoal().getListAlternative().get(i).setMark(mark);
        }
        
        StringBuilder sb = new StringBuilder();
        
        for (int i = 0; i < ACStorage.getInstance().getGoal().getListAlternative().size(); i++) {
            sb.append(ACStorage.getInstance().getGoal().getListAlternative().get(i).getName()).append(" has been weighted with: ").append(ACStorage.getInstance().getGoal().getListAlternative().get(i).getMark()).append("\n");
        }
        
        PnlFinalResultsPreview pfrp = new PnlFinalResultsPreview();
        pfrp.getTxtAreaResultsPreview().setText(sb.toString());
        
        setActivePanel(pfrp);
    }//GEN-LAST:event_btnCalculateFinalResultsActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmFinalResults.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmFinalResults.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmFinalResults.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmFinalResults.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                FrmFinalResults dialog = new FrmFinalResults(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCalculateFinalResults;
    private javax.swing.JPanel pnlFinalResults;
    // End of variables declaration//GEN-END:variables

    public void setActivePanel(JPanel noviPanel) {
        if (pnlFinalResults != null) {
            this.remove(pnlFinalResults);
        }
        
        pnlFinalResults = noviPanel;
        getContentPane().add(pnlFinalResults, java.awt.BorderLayout.CENTER);
        pnlFinalResults.setVisible(true);
        validate();
        repaint();
        pack();
    }
}
